package com.jvm;

import java.io.FileInputStream;

public class MyClassLoaderTest {
    static class MyClassLoader extends ClassLoader{
        private String classPath;

        public MyClassLoader(String classPath) {
            this.classPath = classPath;
        }

        protected byte[] loadByte(String name) {
            name = name.replace("\\.", "\")");
            FileInputStream fis = null;
            byte[] data = null;
            try {
                fis = new FileInputStream(classPath + "/" + name + ".class");
                int len = fis.available();
                data = new byte[len];
                fis.read(data);
                fis.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally {
                return data;
            }
        }

        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            byte[] data = loadByte(name);
            //defineClass将一个字节数组转为Class对象，
            // 这个字节数组是class文件读取后最终的字节数组
            return defineClass(name, data, 0, data.length);
        }
    }

    public static void main(String[] args) {

    }

}
